package com.itsu.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itsu.dao.CompanyDao;
import com.itsu.dao.DeptDao;
import com.itsu.domain.Company;
import com.itsu.domain.Dept;

import com.itsu.service.DeptService;
import com.itsu.utils.MapperUtils;

import java.util.List;

public class DeptServiceImpl implements DeptService {

    private DeptDao mapper;

    @Override
    public void save(Dept dept) {
        try {
            mapper = MapperUtils.getMapper(DeptDao.class);
            mapper.save(dept);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void delete(Dept dept) {
        try {
            mapper = MapperUtils.getMapper(DeptDao.class);
            mapper.delete(dept);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void update(Dept dept) {
        try {
            mapper = MapperUtils.getMapper(DeptDao.class);
            mapper.update(dept);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public Dept findById(String id) {
        try {
            mapper = MapperUtils.getMapper(DeptDao.class);
            Dept dept = mapper.findById(id);
            return dept;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public List<Dept> findAll() {
        try {
            mapper = MapperUtils.getMapper(DeptDao.class);
            List<Dept> all = mapper.findAll();
            return all;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public PageInfo<Dept> findAll(int pageNum, int pageSize) {
        try {
            mapper = MapperUtils.getMapper(DeptDao.class);
            PageHelper.startPage(pageNum, pageSize);
            List<Dept> all = mapper.findAll();
            PageInfo<Dept> deptPageInfo = new PageInfo<>(all);
            return deptPageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }
}
